Page({
  data: {
    carList: [],
  },

  async onLoad(options) {
    await this.loadOrderDetail();
  },

  goToOrderDetailPage(event) {
    const orderId = event.currentTarget.dataset.id;
    const orderStatus = event.currentTarget.dataset.status;

    if (orderStatus === '2') {
      wx.showToast({
        title: '订单已删除',
        icon: 'none',
        duration: 2000,
      });
    } else {
      wx.navigateTo({
        url: `/pages/orderDetail/orderDetail?id=${orderId}`,
      });
    }
  },

  loadOrderDetail() {
    wx.request({
      url: 'http://1.94.51.11:8850/user/queryOrders',
      header: {
        Cookie: wx.getStorageSync('session'),
      },
      success: res => {
        if (res.data.code != 200) {
          wx.navigateBack();
        } else {
          const carList = res.data.data.map(item => {
            let tag;
            switch (item.status) {
              case '1':
                tag = '进行中';
                break;
              case '0':
                tag = '已完成';
                break;
              case '2':
                tag = '已删除';
                break;
              default:
                tag = '';
            }
            return {
              ...item,
              tag,
              title: item.title || '', // 默认值处理
              num: item.num || 0, // 默认值处理
              price: item.price || '0.0', // 默认值处理
              version: item.version || '', // 默认值处理
              colorImg: item.colorImg || '', // 默认值处理
            };
          });
          this.setData({
            carList: carList,
          });
          console.log(this.data.carList);
        }
      },
      fail: error => {
        console.log(error);
      },
    });
  },
});
